Functions In C++

 Functions In C++

Introduction-

  •  A function is a group of statements that together perform a task. Every C++ program has at least onefunction, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions.

Main()function-

  •  the role of main() is to indicate to the compiler to convert source code from { open curly bracket to } close curly bracket.

Prototyping-

  •  The Prototyping Model is a systems development method (SDM) in which a prototype (an early approximation of a final system or product) is built, tested, and then reworked as necessary until an acceptable prototype is finally achieved from which the complete system or product can now be developed.

Call and return by reference-

          #include<iostram.h>
          int sum(int *a,int *b);
          void main()
         {
         int a=5,b=6;
         int c;
         c=sum(&a,&b);
         cout<<"Sum of Two NUmber="<<c;
         getch();
         }
         int sum(int *a,int *b)
        {
         return *a+*b;
         }

Inline function-

  •  The inline functions are a C++ enhancement feature to increase the execution time of a program.
  •  Functions can be instructed to compiler to make them inline so that compiler can replace those function definition wherever those are being called.
          NOTE- This is just a suggestion to compiler to make the function inline, if function is big (in                  term of executable instruction etc) then, compiler can ignore the “inline” request and treat the                  function as normal function.

The syntax for defining the function inline is:-
inline return-type function-name(parameters)
{
// function code
}

 #include <iostream>
  • using namespace std;
          inline int cube(int s)
         {
         return s*s*s;
         }
         int main()
        {
         cout << "The cube of 3 is: " << cube(3) << "\n";
         return 0;
         } //Output: The cube of 3 is: 27

Default arguments-

  •  A default argument is a value provided in function declaration that is automatically assigned by the compiler if caller of the function doesn't provide a value for the argument with default value. Following is a simple C++ example to demonstrate use of default arguments.

// A function with default arguments, it can be called with
// 2 arguments or 3 arguments or 4 arguments.
int sum(int x, int y, int z=0, int w=0)
{
    return (x + y + z + w);
}
/* Drier program to test above function*/
int main()
{
    cout << sum(10, 15) << endl;
    cout << sum(10, 15, 25) << endl;
    cout << sum(10, 15, 25, 30) << endl;
    return 0;
}

function overloading

  •  C++ allows specification of more than one function of the same name in the same scope. These are called overloaded functions and are described in detail in Overloading. Overloaded functions enable programmers to supply different semantics for a function, depending on the types and number of arguments.

                             #include <iostream>
                            using namespace std;
                            void print(int i) {
                                cout << " Here is int " << i << endl;
                            }
                            void print(double f) {
                                cout << " Here is float " << f << endl;
                            }
                            void print(char* c) {
                               cout << " Here is char* " << c << endl;
                            }
                            int main() {
                                print(10);
                                print(10.10);    
                                print("ten");
                                return 0;
                            }

friend functions-

  •  C++ Friend Functions. A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
                        #include <iostream>
                        class A {
                        private:
                            int a;
                        public:
                            A() { a=0; }
                            friend class B; // Friend Class
                        };
                        class B {
                        private:
                            int b;
                        public:
                            void showA(A& x) {
                                // Since B is friend of A, it can access
                                // private members of A
                                std::cout << "A::a=" << x.a;
                            }
                        };
                        int main() {
                            A a;
                            B b;
                            b.showA(a);
                            return 0;

                        }

Private Member Functions-


                      # include <iostream.h>
                      # include <conio.h>
                      class student
                     {
                       private:
                            int rn;
                            float fees;
                            void read()
                         {
                            rn=12;    
                            fees=145.10;
                         }
                      public:
                            void show()
                        {
                            read();
                            cout<<"\n Rollno = "<<rn;
                            cout<<"\n Fees = "<<fees;
                        }
                    };
                       void main ( )
                       {    
                            clrscr ( );
                            student st;
                            // st.read ( ); // not accessible
                            st.show ( );
                            getch();
                        }

Various storage classes-

  •  A storage class defines the scope (visibility) and life-time of variables and/or functions within a C++ Program. These specifiers precede the type that they modify. There are following storage classes, which can be used in a C++ Program= 
              o Auto
              o register
              o static
              o extern
              o mutable

The auto Storage Class-

  • The auto storage class is the default storage class for all local variables.
{
int mount;
auto int month;
}

The register Storage Class-

  • The register storage class is used to define local variables that should be stored in a register instead of RAM.
{
register int miles;
}

The static Storage Class-

  •  The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope.
#include <iostream>
// Function declaration
void func(void);
static int count = 10; /* Global variable */
main() {
while(count--) {
func();
}
return 0;
}
// Function definition
void func( void ) {
static int i = 5; // local static variable
i++;
std::cout << "i is " << i ;
std::cout << " and count is " << count << std::endl;
}

The extern Storage Class-

  •  The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern' the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.

The mutable Storage Class-

  •  The mutable specifier applies only to class objects, which are discussed later in this tutorial. It allows a member of an object to override const member function.

Static member functions-

  •  A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator.

Post a Comment

Previous Post Next Post